﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace NLBS.Urls
{
    /// <summary>
    /// Manages Url rewriting
    /// </summary>
    public class UrlReWriteProvider
    {
        Regex _reWriteFilter = null;
        LocationSet _locations = null;

        /// <summary>
        /// Instantiates a UrlRewriteProvider
        /// </summary>
        /// <param name="locations">LocationSet of locations to include in Url rewriting</param>
        /// <param name="fileExpression">Regex pattern of matching Urls subject to rewriting</param>
        public UrlReWriteProvider(LocationSet locations, string fileExpression)
        {
            _locations = locations;
            _reWriteFilter = new Regex(fileExpression, RegexOptions.IgnoreCase | RegexOptions.Compiled);

        }

        /// <summary>
        /// Returns the rewritten Url for the given HttpContext
        /// </summary>
        /// <param name="context">HttpContext to rewrite</param>
        /// <returns>Rewritten Url for the given HttpContext</returns>
        public virtual string GetRewrittenUrl(HttpContext context)
        {
            string newPath = null;

            string path = context.Request.Path;
           
                IList<Location> locations = _locations.FindLocationsByPath(path);

                foreach (Location location in locations)
                {
                    newPath = location.GetReWrittenUrl(context, path, context.Request.Url.Query);
                    if (newPath != null)
                        break;
                }
            //    if (!_reWriteFilter.IsMatch(path))
            //    {
            //}

            return newPath;
        }

        /// <summary>
        /// Returns the rewritten Url for the path
        /// This method is used to manually rewrite a url for parsing purposes, such as in the Pingback service
        /// It does not use the current HttpContext
        /// </summary>
        /// <param name="path">The path of the HttpRequest</param>
        /// <param name="queryString">The querystring of the HttpRequest</param>
        /// <returns>Rewritten Url for the given path and querystring</returns>
        public virtual string GetRewrittenUrl(string path, string queryString)
        {
            string newPath = null;

            if (!_reWriteFilter.IsMatch(path))
            {
                IList<Location> locations = _locations.FindLocationsByPath(path);

                foreach (Location location in locations)
                {
                    newPath = location.GetReWrittenUrl(path, queryString);
                    if (newPath != null)
                        break;
                }
            }

            return newPath;
        }

        /// <summary>
        /// Performs Url rewriting, assigning the internal rewrite path
        /// </summary>
        /// <param name="context">HttpContext to rewrite</param>
        /// <param name="path">Internal rewrite path</param>
        /// <param name="queryString">Request query string</param>
        public virtual void RewriteUrl(HttpContext context, string path, string queryString)
        {
            context.RewritePath(path, null, queryString, true);
        }
    }
}
